#我的题库页面功能测试
import time
from selenium.webdriver.common.by import By

from common.Utils import QE_Driver

#测试问卷考试系统我的题库页面

class QE_QuestionTest:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://8.155.1.153:8081/repo/index"
        self.driver = QE_Driver.driver
        self.driver.get(self.url)

    #进行页面元素的查找检查
    def QuestionCheckElement(self):
        #检查名称输入标签
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-pro-card.ant-pro-table-search.ant-pro-table-search-query-filter > form > div > div:nth-child(1) > div > div > div.ant-col.ant-form-item-label > label")
        #检查名称输入框
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-pro-card.ant-pro-table-search.ant-pro-table-search-query-filter > form > div > div:nth-child(1) > div > div > div.ant-col.ant-form-item-control > div > div > span")
        #检查类型选择框标签
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-pro-card.ant-pro-table-search.ant-pro-table-search-query-filter > form > div > div:nth-child(2)")
        #检查重置按钮
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-pro-card.ant-pro-table-search.ant-pro-table-search-query-filter > form > div > div:nth-child(3) > div > div > div.ant-col.ant-form-item-control > div > div > div > div > div > div:nth-child(1) > button")
        #检查查询按钮
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-pro-card.ant-pro-table-search.ant-pro-table-search-query-filter > form > div > div:nth-child(3) > div > div > div.ant-col.ant-form-item-control > div > div > div > div > div > div:nth-child(2) > button")
        #检查新建按钮
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div:nth-child(2) > div > div.ant-pro-table-list-toolbar > div > div.ant-space.ant-space-horizontal.ant-space-align-center.ant-pro-table-list-toolbar-right > div:nth-child(1) > div > div:nth-child(1) > button")
        #检查导出按钮
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div:nth-child(2) > div > div.ant-pro-table-list-toolbar > div > div.ant-space.ant-space-horizontal.ant-space-align-center.ant-pro-table-list-toolbar-right > div:nth-child(1) > div > div:nth-child(2) > button")
        #检查题库列表
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div:nth-child(2) > div > div.ant-pro-table-list-toolbar > div > div.ant-pro-table-list-toolbar-left > div")
        #检查文本导入按钮
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div:nth-child(2) > div > div.ant-table-wrapper > div > div > div > div > div > table > tbody > tr:nth-child(2) > td:nth-child(9) > div > div:nth-child(1) > a")
        #检查试题管理按钮
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div:nth-child(2) > div > div.ant-table-wrapper > div > div > div > div > div > table > tbody > tr:nth-child(2) > td:nth-child(9) > div > div:nth-child(3) > a")

        #添加屏幕截图
        QE_Driver.getScreenshot()

        # 功能测试
        def QuestionFunctionTest(self):
            type = "考试"

            # 测试新建功能

            # 点击新建按钮
            self.driver.find_element(By.CSS_SELECTOR,
                                     "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div:nth-child(2) > div > div.ant-pro-table-list-toolbar > div > div.ant-space.ant-space-horizontal.ant-space-align-center.ant-pro-table-list-toolbar-right > div:nth-child(1) > div > div:nth-child(1) > button").click()
            # 添加屏幕截图
            QE_Driver.getScreenshot()
            # 输入题库名称

            # 切换到点击新建之后的页面之中，因为新建之后的页面里面的元素的id和没点击新建的时候的元素是一样的，所以要限制一下查找元素的范围
            drawer = self.driver.find_element(By.CLASS_NAME, "ant-drawer-wrapper-body")
            # 后续使用drawer 进行查找元素操作，意思就是在ant-drawer-body元素范围内进行查找元素
            drawer.find_element(By.CSS_SELECTOR, "#name").send_keys("测试新增题库功能")
            # 添加屏幕截图
            QE_Driver.getScreenshot()
            time.sleep(3)
            # 选择题库类型
            drawer.find_element(By.CSS_SELECTOR, "#mode").click()
            time.sleep(1)
            # 添加屏幕截图
            QE_Driver.getScreenshot()
            # 选择问卷
            select_option = drawer.find_element(By.CLASS_NAME, "rc-virtual-list-holder-inner")
            option = select_option.find_element(By.XPATH, f'.//div[@title="问卷"]')
            self.driver.execute_script("arguments[0].scrollIntoView(true);", option)
            self.driver.execute_script("arguments[0].click();", option)
            # 这里并不是一个select组件，不能直接使用Select
            time.sleep(2)
            # 添加屏幕截图
            QE_Driver.getScreenshot()
            time.sleep(1)
            # 点击确定按钮
            drawer.find_element(By.XPATH, "/html/body/div[4]/div/div[3]/div/div/div[3]/div/div/div[2]/button").click()
            # 添加屏幕截图
            QE_Driver.getScreenshot()
            # 检查是否回到题库列表页面，如果是则创建题库成功，否则创建失败
            self.driver.find_element(By.CSS_SELECTOR,
                                     "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-pro-card.ant-pro-table-search.ant-pro-table-search-query-filter > form > div > div:nth-child(3) > div > div > div.ant-col.ant-form-item-control > div > div > div > div > div > div:nth-child(2) > button")
            # 添加屏幕截图
            QE_Driver.getScreenshot()

            ##########################################################################################################################

            # 测试查询功能

            # 名称输入框输入题库名称“测试新增题库功能”
            self.driver.find_element(By.CSS_SELECTOR, "#name").send_keys("测试新增题库功能")
            # 添加屏幕截图
            QE_Driver.getScreenshot()
            # 点击查询按钮
            self.driver.find_element(By.CSS_SELECTOR,
                                     "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-pro-card.ant-pro-table-search.ant-pro-table-search-query-filter > form > div > div:nth-child(3) > div > div > div.ant-col.ant-form-item-control > div > div > div > div > div > div:nth-child(2) > button").click()
            time.sleep(1)
            # 添加屏幕截图
            QE_Driver.getScreenshot()
            # 验证是否查询到，如果题库列表有输出则查询成功，否则查询失败
            self.driver.find_element(By.CSS_SELECTOR,
                                     "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div:nth-child(2) > div > div.ant-table-wrapper > div > div > div > div > div > table > tbody > tr.ant-table-row.ant-table-row-level-0 > td:nth-child(1) > div")
            time.sleep(1)
            # 添加屏幕截图
            QE_Driver.getScreenshot()

            ##########################################################################################################################

            # 测试重置功能

            # 点击重置按钮
            self.driver.find_element(By.CSS_SELECTOR,
                                     "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-pro-card.ant-pro-table-search.ant-pro-table-search-query-filter > form > div > div:nth-child(3) > div > div > div.ant-col.ant-form-item-control > div > div > div > div > div > div:nth-child(1) > button").click()
            time.sleep(1)
            # 添加屏幕截图
            QE_Driver.getScreenshot()
            # 验证是否重置成功，如果名称输入框为空则代表重置成功，否则重置失败
            actual = self.driver.find_element(By.CSS_SELECTOR, "#name").text
            # 添加屏幕截图
            QE_Driver.getScreenshot()
            # 断言检测一下是否符合预期
            assert actual == ""

    # question = QE_QuestionTest()
    # question.QuestionCheckElement()
    # question.QuestionFunctionTest()

    # 指定浏览器的退出
    # QE_Driver.driver.quit()